package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import model.CommonList;
import model.ShortDate;

public class DaoImp_CommonList implements IDao_CommonList
{
	private DaoMethod method;

	// private CommonList common;

	public DaoImp_CommonList()
	{
		method = new DaoMethod();
		// common = CommonList.getInstance();
	}

	public Map<String, Integer> getSchool()
	{
		String exe = "select school_name,id from school";
		return getMap(exe);
	}

	public Map<String, Integer> getClassroom()
	{
		String exe = "select classroom_name,id from classroom";
		return getMap(exe);
	}

	public Map<String, Integer> getClassroom_class()
	{
		String exe = "select cr.classroom_name,c.id from classroom cr,class c where c.classroom_id=cr.id and c.isend=0";
		return getMap(exe);
	}

	public Map<String, Integer> getDormNumber()
	{
		String exe = "select number,id from dorm where isuse=0";
		return getMap(exe);
	}

	public Map<String, Integer> getMajor()
	{
		String exe = "select major_name,id from major";
		return getMap(exe);
	}

	public Map<String, Integer> getStudentName()
	{
		String exe = "select name,id from student where isout=0";
		return getMap(exe);
	}

	public Map<String, Integer> getTeacher()
	{
		String exe = "select teacher_name,id from teacher";
		return getMap(exe);
	}

	public Map<String, Integer> getType()
	{
		String exe = "select type_name,id from type";
		return getMap(exe);
	}

	public Vector<String> getShortDateVec()
	{
		String exe = "select date_type_in from meter group by date_type_in order by date_type_in desc";
		Vector<Vector<Object>> tempshortDateVec = method.stQuery(exe);
		if (tempshortDateVec.size() > 0)
		{
			Vector<String> shortDateVec = new Vector<String>();
			ShortDate shortDate = new ShortDate();
			for (Iterator iterator = tempshortDateVec.iterator(); iterator
					.hasNext();)
			{
				Vector<Object> vector = (Vector<Object>) iterator.next();
				shortDate.setDate((Date) vector.get(0));
				shortDateVec.add(shortDate.getShortDate());
			}
			return shortDateVec;
		}
		else
		{
			return new Vector<String>();
		}
	}

	public float[] getWaterAndPowerPrice()
	{
		String exe = "select waterprice,powerprice from meterprice order by date_modify limit 1";
		Vector<Vector<Object>> result = method.stQuery(exe);
		float[] f = new float[2];
		try
		{
			f[0] = Float.valueOf(result.get(0).get(0).toString());
			f[1] = Float.valueOf(result.get(0).get(1).toString());

		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			f[0] = 0;
			f[1] = 0;
		}
		return f;
	}

	public int getRentPerMonth()
	{
		String exe = "select rentPerMonth from rentprice order by date_modify desc limit 1";
		Vector<Vector<Object>> rentVec = method.stQuery(exe);
		try
		{
			return (Integer) rentVec.get(0).get(0);
		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			return 0;
		}
	}

	// public void getCommonList()
	// {
	// CommonList common = CommonList.getInstance();

	// String exe_teacher = "select teacher_name,id from teacher";
	// String exe_school = "select school_name,id from school";
	// String exe_studentName = "select name,id from student";
	// String exe_major = "select major_name,id from major";
	// String exe_type = "select type_name,id from type";
	// String exe_classroom_class = "select cr.classroom_name,c.id from
	// classroom cr,class c where c.classroom_id=cr.id";

	// common.setTeacher(getMap(exe_teacher));
	// common.setSchool(getMap(exe_school));
	// common.setStudentName(getMap(exe_studentName));
	// common.setMajor(getMap(exe_major));
	// common.setType(getMap(exe_type));
	// common.setClassroom_class(getMap(exe_classroom_class));
	// String exe_water_power_price = "select waterprice,powerprice from
	// meterprice order by date_modify";
	// Vector<Vector<Object>> result =
	// method.stQuery(exe_water_power_price);
	// try
	// {
	// common
	// .setWaterPrice(Float.valueOf(result.get(0).get(0)
	// .toString()));
	// common
	// .setPowerPrice(Float.valueOf(result.get(0).get(1)
	// .toString()));
	// }
	// catch (ArrayIndexOutOfBoundsException e)
	// {
	// common.setWaterPrice(0.0f);
	// common.setPowerPrice(0.0f);
	// }
	// String exe_shortDateVec = "select date_type_in from meter group by
	// date_type_in order by date_type_in desc";
	// Vector<Vector<Object>> tempshortDateVec = method
	// .stQuery(exe_shortDateVec);
	// Vector<String> shortDateVec = new Vector<String>();
	// ShortDate shortDate = new ShortDate();
	// for (Iterator iterator = tempshortDateVec.iterator(); iterator
	// .hasNext();)
	// {
	// Vector<Object> vector = (Vector<Object>) iterator.next();
	// shortDate.setDate((Date) vector.get(0));
	// shortDateVec.add(shortDate.getShortDate());
	// }
	// common.setShortDateVec(shortDateVec);
	// }

	private HashMap<String, Integer> getMap(String exe)
	{
		Connection conn = DaoMethod.getConnection();
		Statement st = null;
		ResultSet rs = null;
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		try
		{
			st = conn.createStatement();
			rs = st.executeQuery(exe);
			if (rs.next())
			{
				do
				{
					map.put(rs.getString(1), rs.getInt(2));
				}
				while (rs.next());
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				if (rs != null)
				{
					rs.close();
				}
			}
			catch (SQLException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try
			{
				if (st != null)
				{
					st.close();
				}
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return map;
	}
}
